package edu.maping.easy.painter.plugins;

import controlP5.ControlEvent;
import controlP5.ControlGroup;
import processing.core.PApplet;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.PhysicPolyThing;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.sequence.ActionConf;
import edu.maping.easy.sequence.ticker.TickerListener;

public class ThingPainterRectangleTransform3D implements ThingPainterDelegate,
		TickerListener {

	int moment = 0;

	boolean asc = false;

	int time = 0;

	private UtilPainter utilPainter = new UtilPainter();

	int blue = 0;
	int green = 0;
	int red = 0;

	public void draw(Thing cosa, PApplet pApplet, Object object) {

		time++;

		if (cosa.isEvent()) {
			turnOn();
			cosa.setEvent(false);
		}

		if (asc) {
			if (moment < 255) {
				moment += 20;
			} else {
				asc = false;
				moment = 255;
			}
		} else {
			if (moment > 0) {
				moment -= 3;
			} else {
				moment = 0;
				return;
			}
		}

		float tempR = (red / 255F) * moment;
		float tempG = (green / 255F) * moment;
		float tempB = (blue / 255F) * moment;

		PolygonThing poly = (PolygonThing) cosa;
		pApplet.pushStyle();
		pApplet.lights();
		// pApplet.camera(30F, (float)pApplet.mouseY, 220.0F, // eyeX, eyeY,
		// eyeZ
		// 0.0F, 0.0F, 0.0F, // centerX, centerY, centerZ
		// 0.0F, 1.0F, 0.0F);
		pApplet.beginShape(PApplet.QUAD_STRIP);
		pApplet.strokeWeight(0);
		pApplet.stroke(0);
		pApplet.fill(tempR, tempG, tempB);
		// pintamos laterales

		int posZ = pApplet.mouseX - 100;

		for (int i = 0; i < poly.getVertex().size(); i++) {
			Point point = poly.getVertex().get(i);
			if (posZ < 0) {
				// pApplet.vertex(point.x, point.y, pApplet.mouseX - 100);
				// pApplet.vertex(point.x, point.y, 0);
			} else {
				pApplet.vertex(point.x, point.y, 0);
				pApplet.vertex(point.x, point.y, pApplet.mouseX - 100);
			}
		}
		// pintamos frontal
		pApplet.endShape();
		pApplet.beginShape();
		for (int i = 0; i < poly.getVertex().size(); i++) {
			Point point = poly.getVertex().get(i);
			pApplet.vertex(point.x, point.y, posZ);
		}
		pApplet.endShape();
		pApplet.popStyle();

	}

	public void draw(Thing body, PApplet pApplet) {
		draw(body, pApplet, null);
	}

	public void turnOn() {
		asc = true;
	}

	@Override
	public void setConf(ActionConf actionConf) {
		for (int i = 0; i < utilPainter.size(); i++) {
			Slider slider = (Slider) utilPainter.get(i);
			String key = slider.name;
			if (actionConf.containsKey(key)) {
				float value = actionConf.get(key);
				setParameters(key, value);
				utilPainter.setParameter(key, value);
			} else {
				utilPainter.setParameter(key, 0);
			}
		}

	}

	@Override
	public String getName() {
		return "Pulso3D";
	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		EventPainter eventPainter = utilPainter.controlEvent(theEvent);
		if (eventPainter != null)
			setParameters(eventPainter.name, eventPainter.value);
		return eventPainter;
	}

	private void setParameters(String name, float value) {

		if (name.equals("RED")) {
			red = (int) value;
		} else if (name.equals("GREEN")) {
			green = (int) value;
		} else if (name.equals("BLUE")) {
			blue = (int) value;
		}
	}

	@Override
	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {
		utilPainter.setup(pApplet, controlFacade, buttonList, guiFactory);
		utilPainter.add("RED", 0, 255);
		utilPainter.add("GREEN", 0, 255);
		utilPainter.add("BLUE", 0, 255);
	}

	@Override
	public void showForm(int x, int y) {
		utilPainter.setPosition(x + 50, y);
		utilPainter.render();
	}

	@Override
	public void hideForm() {
		utilPainter.hide();
	}

	public ActionConf createNewActionConf() {
		return utilPainter.createActionConf();
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		// TODO Auto-generated method stub
		return null;
	}
}
